package com.itheima.xiancheng.demo4;

public class Demo {
    public static void main(String[] args) {
        Box box = new Box();
        int day = 30;
        //送奶工人
        new Thread(() -> {
            synchronized (box) {
                for (int i = 1; i <= day; i++) {
                    if (box.isFlag()) {
                        try {
                            box.wait();
                            i--;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        box.put(i);
                        box.setFlag(true);
                        box.notify();
                    }
                }
            }
        }).start();
        //地主家 的少爷
        new Thread(() -> {
            synchronized (box) {
                for (int i = 1; i <= day; i++) {
                    if (box.isFlag()) {
                        box.take();
                        box.setFlag(false);
                        box.notify();
                    } else {
                        try {
                            box.wait();
                            i--;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }).start();

    }
}
